/* 
 * Copyright 2014 ShangDao.Ltd  All rights reserved.
 * SiChuan ShangDao.Ltd PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 * 
 * @ActBaLists.java  2014-9-16 下午8:31:34 - Carson
 * @author Guzhu
 * @email:muyi126@163.com
 * @version 1.0
 */

package com.android.tiango.baba.act;

import java.util.ArrayList;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.android.scsd.tiango.R;
import com.android.support.httpclient.HttpCallBack;
import com.android.support.httpclient.HttpClientAsync;
import com.android.support.httpclient.HttpParams;
import com.android.tiango.baba.BaListActivity;
import com.android.tiango.baba.cookie.HttpUrl;
import com.android.tiango.baba.cookie.ShareCookie;
import com.android.tiango.baba.entity.BarModel;
import com.android.tiango.baba.entity.BaseEntity;
import com.android.tiango.baba.entity.SearchBarEntity;
import com.android.tiango.baba.util.Constants;
import com.android.tiango.baba.util.StringUtils;
import com.android.tiango.baba.util.ToastUtils;
import com.android.tiango.baba.view.PullListView.PullListViewListener;
import com.android.tiango.baba.view.RoundCornerImageView;

/**
 * 贴吧列表
 * @author YW_pc
 * 
 */
public class ActBaLists extends BaListActivity<BarModel> implements PullListViewListener {
	private String subjectID = "";
	private boolean isRequest = false;
	private String subjectName = "";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Intent intent = getIntent();
		if (intent.hasExtra(Constants.BARID)) {
			subjectID = intent.getStringExtra(Constants.BARID);
			subjectName = intent.getStringExtra(Constants.BARNAME);
		} else {
			this.finish();
		}
		mTitleBar.setTitle(subjectName);
		// mTitleBar.setMoreIcon(R.drawable.search_whit_icon);
		setBackOnClickListener();
		mListView.setPullListener(this);
		mListView.startOnRefresh();
	}

	@Override
	protected View getItemView(int position, View convertView, ViewGroup parent) {
		ViewHolder viewHolder;
		if (convertView == null) {
			convertView = LayoutInflater.from(this).inflate(R.layout.item_myba_join, null);
			viewHolder = new ViewHolder();
			viewHolder.iv_header = (RoundCornerImageView) convertView.findViewById(R.id.iv_header);
			viewHolder.tv_title = (TextView) convertView.findViewById(R.id.tv_title);
			viewHolder.tv_chengYuan = (TextView) convertView.findViewById(R.id.tv_chengYuan);
			viewHolder.tv_huati = (TextView) convertView.findViewById(R.id.tv_huati);
			viewHolder.bt_join = (Button) convertView.findViewById(R.id.bt_join);
			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}
		final BarModel bean = mListData.get(position);
		if (null != bean) {
			mImageLoader.loadImage(bean.getBarIcon(), viewHolder.iv_header);
			viewHolder.tv_title.setText(bean.getBarName());
			viewHolder.tv_chengYuan.setText(bean.getMemberNumber());
			viewHolder.tv_huati.setText(bean.getTodayTopicNumber() + "");
			if (bean.getIsJoin() == 1) {
				viewHolder.bt_join.setText("已加入");
				viewHolder.bt_join.setOnClickListener(null);
				viewHolder.bt_join.setBackgroundColor(getResources().getColor(R.color.font_gray));
			} else {
				viewHolder.bt_join.setText("加入");
				viewHolder.bt_join.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						if (!isRequest) {
							addBar(bean.getBarId());
						}
					}
				});
			}
		}

		return convertView;
	}

	private class ViewHolder {
		private RoundCornerImageView iv_header;
		private TextView tv_title;
		private TextView tv_chengYuan;
		private TextView tv_huati;
		private Button bt_join;

	}

	@Override
	public void onPullRefresh() {
		mPageIndex = 1;
		loadData();

	}

	@Override
	public void onPullLoadMore() {
		mPageIndex += 1;
		loadData();

	}

	private void loadData() {
		HttpClientAsync client = HttpClientAsync.getInstance();
		HttpParams param = new HttpParams();
		param.put("subjectID", subjectID);
		param.put("token", ShareCookie.getToken());
		param.put("pageIndex", mPageIndex);
		param.put("pageSize", Constants.PAGE_SIZE);

		client.get(HttpUrl.getUrl(HttpUrl.SearchTopicsBySubject), param, new HttpCallBack() {

			@Override
			public void onHttpFailure(Exception arg0, String arg1) {
				if (!StringUtils.isEmpty(arg1)) {
					ToastUtils.showMessage(arg1);
				}
				if (mPageIndex > 1) {
					mPageIndex -= 1;
				}
				mListView.onRefreshFinish();
				mListView.onLoadFinish();
			}

			@Override
			public void onHttpStarted() {

			}

			@Override
			public void onHttpSuccess(Object arg0) {
				SearchBarEntity entity = (SearchBarEntity) arg0;
				SearchBarEntity.SearchBarModel data = entity.getData();
				if (null != data) {
					ArrayList<BarModel> list = data.getBarList();
					if (null != list) {
						mAdapter.putData(list);
					}

				}
				mListView.onRefreshFinish();
				mListView.onLoadFinish();
			}

		}, SearchBarEntity.class);// , BarEntity.class

	}

	// 加入吧
	private void addBar(String barId) {
		showWaitingDialog();
		isRequest = true;
		HttpClientAsync client = HttpClientAsync.getInstance();
		HttpParams param = new HttpParams();
		param.put("barId", barId);
		param.put("token", ShareCookie.getToken());
		param.put("type", "1");// 0-退出 1-加入

		client.post(HttpUrl.getUrl(HttpUrl.OperateTopicBar), param, new HttpCallBack() {

			@Override
			public void onHttpSuccess(Object arg0) {
				isRequest = false;
				dismissWaitingDialog();
				BaseEntity entity = (BaseEntity) arg0;
				if (null != entity) {
					ToastUtils.showMessage("加入成功");
					mListView.startOnRefresh();
				}
			}

			@Override
			public void onHttpStarted() {

			}

			@Override
			public void onHttpFailure(Exception arg0, String arg1) {
				isRequest = false;
				if (!StringUtils.isEmpty(arg1)) {
					ToastUtils.showMessage(arg1);
				}
				dismissWaitingDialog();
			}
		}, BaseEntity.class);//
	}
}
